package sskj.regulatory.presentation.screen.signature.signedlist

import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.OnLifecycleEvent
import isskj.regulatory.presentation.base_mvp.api.ApiPresenter
import sskj.regulatory.R
import sskj.regulatory.di.scope.PerActivity
import sskj.regulatory.domain.entity.homesection.HomeSection
import sskj.regulatory.domain.entity.sitevideo.Site
import sskj.regulatory.domain.entity.sitevideo.SiteVideo
import sskj.regulatory.domain.fetcher.Status
import sskj.regulatory.domain.fetcher.result_listener.RequestType
import sskj.regulatory.domain.interactor.SiteVideoInteractor
import java.sql.Types.REF
import javax.inject.Inject


@PerActivity
class SiteVideoListPresenter @Inject constructor(
        private val siteVideoInteractor: SiteVideoInteractor
) : ApiPresenter<SiteVideoListContract.View>(), SiteVideoListContract.Presenter {


    private var page: Int = 1

    private val pageCount: Int = 6

    private val requestType = SITEVIDEO

    private var type = RequestType.PAGE

    override fun reLoad() {
        page = 1
        type = RequestType.REF
        fetchShotList()
    }

    override fun initData() {
        page = 1
        view.showInitLoading()
        fetchShotList()
    }

    override fun onPresenterDestroy() {
        super.onPresenterDestroy()
        fetcher.removeRequest(requestType)
    }


    private fun fetchShotList() {
        //测试数据
//        var siteList = ArrayList<Site>()
//        var sitevideoList = ArrayList<SiteVideo>()
//        sitevideoList.add(SiteVideo("7d14f556b8df4dc2912cc15c732f0201","摄像头1","38ae4d04f88c47c28b069d41b4878e71"))
//        sitevideoList.add(SiteVideo("aa621012455d4cf58e5216bac5d94b97","摄像头2","aa621012455d4cf58e5216bac5d94b97"))
//        sitevideoList.add(SiteVideo("7d14f556b8df4dc2912cc15c732f0201","摄像头3","38ae4d04f88c47c28b069d41b4878e71"))
//        sitevideoList.add(SiteVideo("aa621012455d4cf58e5216bac5d94b97","摄像头4","aa621012455d4cf58e5216bac5d94b97"))
//        sitevideoList.add(SiteVideo("7d14f556b8df4dc2912cc15c732f0201","摄像头5","38ae4d04f88c47c28b069d41b4878e71"))
//        var site1=Site("保利时代","code1","形象进度1","2018-04-25",
//                "保利时代建设单位","保利时代施工单位",
//                "保利时代监理单位","很大", R.mipmap.test1,sitevideoList)
//        var site2=Site("中建之星","code2","形象进度2","2018-04-26",
//                "中建之星","中建之星施工单位",
//                "中建之星监理单位","一般大",R.mipmap.test2,sitevideoList)
//        var site3=Site("碧桂园","code3","形象进度3","2018-04-27",
//                "碧桂园建设单位","碧桂园施工单位",
//                "碧桂园监理单位","非常大",R.mipmap.test3,sitevideoList)
//        var site4=Site("工地4","code4","形象进度4","2018-04-28",
//                "上上城建设单位","上上城施工单位",
//                "上上城监理单位","巨无霸",R.mipmap.test1,sitevideoList)
//        var site5=Site("都市一号","code5","形象进度5","2018-04-29",
//                "都市一号建设单位","都市一号施工单位",
//                "都市一号监理单位","大出天际",R.mipmap.test2,sitevideoList)
//        siteList.add(site1)
//        siteList.add(site2)
//        siteList.add(site3)
//        siteList.add(site4)
//        siteList.add(site5)
//        view.onSiteListReceive(siteList)
//        val success = { it: List<Site> ->
//            if (it.isNotEmpty()) {
//                when (type) {
//                    RequestType.REF -> view.clearSites()
//                    else -> view.onSiteListReceive(it)
//                }
//            } else {
//                view.showNoSites()
//            }
//        }
//
        fetch(siteVideoInteractor.siteVideoList(pageCount, page), SITEVIDEO) {
            if (it.isNotEmpty()) {
                when (type) {
                    RequestType.REF -> {
                        view.clearSites()
                        view.onSiteListReceive(it)
                    }
                    else -> view.onSiteListReceive(it)
                }
            } else {
                view.showNoSites()
            }
        }

    }

    override fun onRequestError(errorCode: String?, errorMessage: String?) {
        view.showError()
    }

    override fun loadMore() {
        page += 1
        type = RequestType.PAGE
        fetchShotList()
    }

}